//
//  BaseConfig.swift
//  HFUTER
//
//  Created by HD on 15/2/3.
//  Copyright (c) 2015年 HD. All rights reserved.
//

import Foundation

class BaseConfig {
    var defaults: NSUserDefaults! = NSUserDefaults.standardUserDefaults()

    ///保存integer类型
    func saveValue(value: Int, key: String) {
        self.defaults.setInteger(value, forKey: key)
    }
    
    ///保存integer64类型
    func saveValue(value: Int64, key: String) {
        self.defaults.setValue("\(value)", forKey: key)
    }
    
    ///保存string类型数据
    func saveValue(value: String, key: String) {
        self.defaults.setValue(value, forKey: key)
    }
    
    ///保存bool类型数据
    func saveValue(value: Bool, key: String) {
        self.defaults.setBool(value, forKey: key)
    }
    
    ///保存float类型数据
    func saveValue(value: Float, key: String) {
        self.defaults.setFloat(value, forKey: key)
    }
    ///保存double类型数据
    func saveValue(value: Double, key: String) {
        self.defaults.setDouble(value, forKey: key)
    }
    
    ///获取integer类型数据
    func getInteger(key: String, defaultValue: Int) -> Int {
        let value: AnyObject? = self.defaults.objectForKey(key)
        return value == nil ? defaultValue : value as! Int
    }
    
    ///获取integer类型数据
    func getInt64(key: String, defaultValue: Int64) -> Int64 {
        let value: AnyObject? = self.defaults.objectForKey(key)
        let int64 = (value == nil ? "\(defaultValue)" : value as! String)
        return (int64 as NSString).longLongValue
    }
    
    ///获取string类型数据
    func getString(key: String, defaultValue: String!) -> String! {
        let value: AnyObject? = self.defaults.objectForKey(key)
        return value == nil ? defaultValue : value as! String
    }
    
    ///获取bool类型数据
    func getBool(key: String, defaultValue: Bool) -> Bool {
        let value: AnyObject? = self.defaults.objectForKey(key)
        return value == nil ? defaultValue : value as! Bool
    }
    
    ///获取float类型数据
    func getFloat(key: String, defaultValue: Float) -> Float {
        let value: AnyObject? = self.defaults.objectForKey(key)
        return value == nil ? defaultValue : value as! Float
    }
    
    ///获取bool类型数据
    func getDouble(key: String, defaultValue: Double) -> Double {
        let value: AnyObject? = self.defaults.objectForKey(key)
        return value == nil ? defaultValue : value as! Double
    }
    
    ///移除一个数据
    func removeKey(key: String) {
        self.defaults.removeObjectForKey(key)
    }
    
    ///是否含有key
    func hasKey(key: String) -> Bool {
        return defaults.objectForKey(key) != nil
    }
    
    ///保存
    func save() {
        self.defaults.synchronize()
    }
}